<template>
	<template v-if="isAlready">
		<index-setting  :model="model"></index-setting>
		<print-tag-setting :model="model"></print-tag-setting>
		<asset-check-setting :model="model"></asset-check-setting>
		<document-setting :model="model"></document-setting>
		<asset-category-summary-setting :model="model"></asset-category-summary-setting>
		<minio-server-setting :model="model"></minio-server-setting>
		<oa-document-server-setting :model="model"></oa-document-server-setting>
	</template>
	<div style="flex: auto;text-align: center">
		<el-button plain type="primary" @click="manage" :loading="loading">保 存</el-button>
		<el-button plain type="info" @click="closePage" :loading="loading">关 闭</el-button>
	</div>
</template>

<script>
import CardContent from "@/views/asset/card/card_content.vue";
import useTabs from "@/utils/useTabs";
import IndexSetting from "@/views/setting/globalparam/index_setting.vue";
import constant from "@/constant";
import PrintTagSetting from "@/views/setting/globalparam/print_tag.vue";
import MinioServerSetting from "@/views/setting/globalparam/minio_server_setting.vue";
import AssetCheckSetting from "@/views/setting/globalparam/asset_check.vue";
import DocumentSetting from "@/views/setting/globalparam/document_setting.vue";
import AssetCategorySummarySetting from "@/views/setting/globalparam/assetCategorySummary_setting.vue";
import OaDocumentServerSetting from "@/views/setting/globalparam/oa_document_server_setting.vue";

export default {
	name: '参数设置',
	components: {
		OaDocumentServerSetting,
		AssetCategorySummarySetting,
		DocumentSetting, AssetCheckSetting, MinioServerSetting, PrintTagSetting, IndexSetting, CardContent},
	data() {
		return {
			loading: false,
			isAlready:false,
			model: {}
		}
	},
	created() {
		this.loadParams();
	},
	mounted() {
	},
	methods: {
		//保存
		closePage() {
			try {
				useTabs.close();
			} finally {
			}
		},
		async loadParams() {
			let res = await this.$API.system.globalParam.load.post({});
			if (res.code === 2000) {
				let params = res.data;
				if (Array.isArray(params)) {
					params.forEach(item => {
						item.paramValue = this.$TOOL.toObj(item.paramValue);
						this.model[item.paramCode] = item;
					});
				}
				this.isAlready = true;
			}
		},
		//导出
		async manage() {
			try {
				this.loading = true;
				let params = [];
				for (let key in this.model) {
					if (params.filter(item => item.paramCode === key).length>0) {
						this.$message.error('参数重复,编码[' + key + ']');
						return;
					}
					let item = this.$TOOL.objCopy(this.model[key]);
					item.paramValue = this.$TOOL.toJsonString(item.paramValue);
					params.push(item);
				}
				console.log(params)
				if (params.length > 0) {
					let res = await this.$API.system.globalParam.manage.post(params);
					if (res.code === 2000) {
						this.$message.success('保存成功');
						this.$store.dispatch("commit",constant.entity.SysGlobalParam.name);
					}
				} else {
					this.$message.warning('请求参数为空');
				}
			} finally {
				this.loading = false;
			}
		},
	}
}
</script>
